﻿package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.greensock.TweenMax;
	import igz.fleaxy.Fleaxy;
	import igz.fleaxy.pattern.Dto;
	import igz.fleaxy.table.ui.TableRowUi;
	import igz.fleaxy.table.ui.TableUi;
	import igz.fleaxy.text.ui.LabelUi;
	import igz.fleaxy.ui.LinkUi;
	import igz.fleaxy.util.DataTableUtil;
	import igz.vd.ui.IconUi;
	import igz.vd.user.UserDto;
	import igz.vd.user.UserManager;

	/**
	 * @author @author Intelygenz | www.intelygenz.com
	 */
	public class test_Table extends bbv11_dai {

		public function test_Table() {
			super();
		}

		override protected function _OnInitComplete() : void {
			super._OnInitComplete();

var data:Object = { "columns":["IdUser@User", "Name@User", "Surname@User", "Email@User", "FkIso3166@User", "FkIso639@User", "FxLevel@User", "IvUser@User", "NsFkCdGrupo@User", "NsFkIdGrupo@User","Edad@User"]
, "rows": [
 ["1", "Jonas", "da Cruzzzzzzzzzzzzzzz", "j@igz.es", "FkIso3166", "ES", "", "", "", "",28]
,["2", "Angel", "Sánchez", "angel@igz.es", "", "ES", "", "", "", "", 22]
,["3", "Pedro", "Sánchez", "pedro@igz.es", "", "ES", "", "", "", "", 25]
,["2", "Angel", "Sánchez", "angel@igz.es", "", "ES", "", "", "", "", 22]
,["3", "Pedro", "Sánchez", "pedro@igz.es", "", "ES", "", "", "", "", 25]
,["2", "Angel", "Sánchez", "angel@igz.es", "", "ES", "", "", "", "", 22]
,["3", "Pedro", "Sánchez", "pedro@igz.es", "", "ES", "", "", "", "", 25]
,["2", "Angel", "Sánchez", "angel@igz.es", "", "ES", "", "", "", "", 22]
,["3", "Pedro", "Sánchez", "pedro@igz.es", "", "ES", "", "", "", "", 25]
,["2", "Angel", "Sánchez", "angel@igz.es", "", "ES", "", "", "", "", 22]
,["3", "Pedro", "Sánchez", "pedro@igz.es", "", "ES", "", "", "", "", 25]
,["2", "Angel", "Sánchez", "angel@igz.es", "", "ES", "", "", "", "", 22]
,["3", "Pedro", "Sánchez", "pedro@igz.es", "", "ES", "", "", "", "", 25]
,["2", "Angel", "Sánchez", "angel@igz.es", "", "ES", "", "", "", "", 22]
,["3", "Pedro", "Sánchez", "pedro@igz.es", "", "ES", "", "", "", "",25]
]
};

			var userList:TableUi = new TableUi();

			userList.$DefineHeader([  { "inner" : "UID"
									  , "order" : "$IdUser"
									  , "width" : 40 
									  }
									, { "inner" : "Nombre"
									  , "order" : "$Name"
									  , "width" : 100 
									  }
									, { "inner" : "Correo"
									  , "order" : "$Email"
									  , "width" : 200 
									  }
									, { "inner" : "Edad"
									  , "order" : "$Edad"
									  , "width" : 50 
									  }
									]
								);

			userList.$DefineData([ { "inner" : "<DTO>$IdUser</DTO>" }
								 , { "inner" : "<DTO>$Name</DTO> <DTO>$Surname</DTO>" }
								 , { "inner" : _DrawEmail }
								 , { "inner" : "<DTO>$Edad</DTO>" }
								 ]
								 , { "rollOver" : _Table_OnRollOver
								   , "rollOut"  : _Table_OnRollOut
								   , "click"    : _Table_OnClick
								   }
								);

			userList.$DefineFooter([ { }
								   , { }
								   , { "inner" : "Total" }
								   , { "inner" :  DataTableUtil.$SumColumn(userManager.$DataTable, "$Edad") }
								   ]
								  );

		    userList.$LoadFromDataTable(data);
			userList.$Draw();

			Fleaxy.$Current.$Stage.addChild( userList );

		}

		private function _Table_OnRollOver(p_rowUi:TableRowUi) : void {
			TweenMax.killTweensOf(p_rowUi);
			TweenMax.to(p_rowUi, 0.3, {colorMatrixFilter:{colorize:0x0099ff, amount:0.4, brightness:2}} );
		}

		private function _Table_OnRollOut(p_rowUi:TableRowUi) : void {
			TweenMax.killTweensOf(p_rowUi);
			TweenMax.to(p_rowUi, 0.5, {colorMatrixFilter:{amount:0}} );
		}

		private function _Table_OnClick(p_rowUi:TableRowUi) : void {
			trace("ROW CLICK!")
		}

		private function _DrawEmail( p_dataTableRow:Array ) : Sprite {
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(0, 0);
			sprite.graphics.drawRect(0, 0, 200, 1);
			sprite.graphics.endFill();
			var labelUi:LabelUi = new LabelUi("__" + p_dataTableRow[3] + "__", "");
			var link:LinkUi = new LinkUi(labelUi, { "onClick" : _clickEmail } );
			sprite.addChild( link );
			return sprite;
		}

		private function _clickEmail(p_mouseEvent:MouseEvent) : void {
			p_mouseEvent.stopPropagation();
			trace("EMAIL CLICK!")
		}
		
	}

}